package email;

import java.util.ArrayList;
import java.util.Arrays;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

public class Util {

	public static long createId(ReceivedMessage message){
		long hash = 0;
		Object tohash = message.getSubject()+message.getBody();
		hash = tohash.hashCode();
		return hash;
	}
	
	public static long createId(Thread thread){
		long hash = 0;
		Object tohash = thread.getName();
		hash = tohash.hashCode();
		return hash;
	}
	
	public static long createId(Object in){
		return in.hashCode();
	}
	
	public static long createId(Message m){
		Object obj = "";
		try {
			obj = m.getSubject()+Util.getBodyContent(m);
		} catch (MessagingException e) {}
		
		return obj.hashCode();
	}
	
	public static String getBodyContent(Message m){
	    String contentReturn;
		String contentType = "";
		Object content;
		String body = "";
		try{
			content = m.getContent();
			contentReturn = m.getContentType();
			
			contentType = determineContentType(contentReturn);
			System.out.println("debug[setBody]:: contentType="+contentType);
			if (contentType.equals("TEXT/PLAIN")){
				body = (String) content;
			}
			else if(contentType.toLowerCase().equals("multipart/alternative")){
				MimeMultipart part = (MimeMultipart)content;
				MimeBodyPart bodypart = (MimeBodyPart)part.getBodyPart(0);
				body = (String)bodypart.getContent();
			}
			else{
				body = "";
			}
		}
		catch(Exception e){}
		return body;
	}
	
	private static String determineContentType(String type){
		String ret = "";
		
		int i = 0;
		//look for a semicolon or the end of the String
		while (i < type.length() && type.charAt(i) != ';'){
			ret += type.charAt(i);
			i++;
		}
		return ret;
	}
	
	public static boolean isThread(ReceivedMessage msg){
		// call the hashing function to get the potential id of a thread
		long id = createId(msg.getSubject()+msg.getFrom()+msg.getDate());
		
		// if there is a thread
		if (ThreadManager.getInstance().getThread(id) != null){
			return true;
		}

		//isn't a thread
		return false;
	}
	
	public static ArrayList<ReceivedMessage> getSortedEmails(ArrayList<ReceivedMessage> messageList){
    	ArrayList<ReceivedMessage> messagesToReturn = new ArrayList<ReceivedMessage>();
    	ReceivedMessage[] messagesToSort = new ReceivedMessage[messageList.size()];
    	for(int i = 0; i < messageList.size(); i++){
    		messagesToSort[i] = (ReceivedMessage)messageList.get(i);
    	}
    	Arrays.sort(messagesToSort, new ReceivedMessageComparator());
    	//messageList.clear();
    	for(int i = 0; i < messagesToSort.length; i++){
    		messagesToReturn.add(messagesToSort[i]);
    	}
    	return messagesToReturn;
    }
	
	public static ArrayList<ReceivedMessage> getReverseSortedEmails(ArrayList<ReceivedMessage> messageList){
		ArrayList<ReceivedMessage> messagesToReturn = new ArrayList<ReceivedMessage>();
    	ReceivedMessage[] messagesToSort = new ReceivedMessage[messageList.size()];
    	for(int i = 0; i < messageList.size(); i++){
    		messagesToSort[i] = (ReceivedMessage)messageList.get(i);
    	}
    	Arrays.sort(messagesToSort, ReceivedMessageComparator.reverseOrder());
    	//messageList.clear();
    	for(int i = 0; i < messagesToSort.length; i++){
    		messagesToReturn.add(messagesToSort[i]);
    	}
    	return messagesToReturn;
	}
	
}
